package com.changgou.business.listener;

import com.changgou.business.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Component //交由spring处理
public class AdListener {

    @Autowired
    private RestTemplate restTemplate;

    @RabbitListener(queues = RabbitMQConfig.AD_UPDATE_QUEQU)
    public void receiveMessage(String postion) {
        System.out.println("接收到消息了:" + postion);

        //nginx发送请求 1 restTemplate
        String url = "http://192.168.200.128/ad_update?position=" + postion;
        Map<String,String> restMap = restTemplate.getForObject(url, Map.class);
        System.out.println(restMap);

        //nginx发送请求 2 okHttp
        /*String url = "http://192.168.200.128/ad_update?position=" + postion;
        //okHttp 客户端
        OkHttpClient okHttpClient = new OkHttpClient();
        //okHttp 请求对象
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        //发送请求
        Call call = okHttpClient.newCall(request);
        //获取结果
        call.enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {
                //Log
                System.out.println("请求失败");
            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log
                ResponseBody body = response.body();
                System.out.println("请求成功,响应体里的内容是:" + body.string());
            }
        });*/
    }
}
